package com.sgj.gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

import com.sgj.common.enums.BizCodeEnum;
import com.sgj.common.vo.MemberRegistVo;
import com.sgj.common.vo.SocialUser;
import com.sgj.common.vo.UserLoginVo;
import com.sgj.gulimall.member.exception.PhoneExistException;
import com.sgj.gulimall.member.exception.UserNameExistException;
import com.sgj.gulimall.member.feign.CouponFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.sgj.gulimall.member.entity.MemberEntity;
import com.sgj.gulimall.member.service.MemberService;
import com.sgj.common.utils.PageUtils;
import com.sgj.common.utils.R;


/**
 * 会员
 *
 * @author guojishen
 * @email 1261550408@qq.com
 * @date 2021-06-15 09:06:47
 */
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("/coupons")
    public R test() {
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("张三");
        R membercoupons = couponFeignService.membercoupon();
        return R.ok().put("member", memberEntity).put("coupons", membercoupons.get("coupons"));
    }

    /**
     * 授权登陆
     * @param socialUser
     * @return
     */
    @PostMapping("/oauth/login")
    public R oAuthLogin(@RequestBody SocialUser socialUser) throws Exception {
        MemberEntity entity = memberService.oAuthLogin(socialUser);
        if (Objects.nonNull(entity)) {
            return R.ok().setData(entity);
        }
        return R.error(BizCodeEnum.LOGINACCOUNT_PASSWORD_INVAILD_EXCEPTION.getCode(), BizCodeEnum.LOGINACCOUNT_PASSWORD_INVAILD_EXCEPTION.getMessage());
    }

    /**
     * 登陆
     * @param vo
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody UserLoginVo vo) {
        MemberEntity entity = memberService.login(vo);
        if (Objects.nonNull(entity)) {
            return R.ok().setData(entity);
        }
        return R.error(BizCodeEnum.LOGINACCOUNT_PASSWORD_INVAILD_EXCEPTION.getCode(), BizCodeEnum.LOGINACCOUNT_PASSWORD_INVAILD_EXCEPTION.getMessage());
    }

    /**
     * 注册
     * @param vo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberRegistVo vo) {
        try {
            memberService.register(vo);
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
        } catch (UserNameExistException e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);
        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}